package com.mj.haier.calc.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.mj.haier.calc.domain.AzEncourage;
import com.mj.haier.calc.repository.AzEncourageRepository;
import com.mj.haier.calc.repository.search.AzEncourageSearchRepository;
import com.mj.haier.calc.service.AzEncourageService;
import com.mj.haier.calc.sharding.generator.ShardingKeyGenerator;
import com.mj.haier.calc.web.rest.errors.BadRequestAlertException;
import com.mj.haier.calc.web.rest.util.HeaderUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing AzEncourage.
 */
@RestController
@RequestMapping("/api")
public class AzEncourageResource {

    private final Logger log = LoggerFactory.getLogger(AzEncourageResource.class);

    private static final String ENTITY_NAME = "microCalcAzEncourage";

    private final AzEncourageRepository azEncourageRepository;

    private final AzEncourageSearchRepository azEncourageSearchRepository;

    private final AzEncourageService azEncourageService;

    public AzEncourageResource(AzEncourageRepository azEncourageRepository,
        AzEncourageSearchRepository azEncourageSearchRepository,
        AzEncourageService azEncourageService) {
        this.azEncourageRepository = azEncourageRepository;
        this.azEncourageSearchRepository = azEncourageSearchRepository;
        this.azEncourageService = azEncourageService;
    }

    /**
     * POST  /az-encourages : Create a new azEncourage.
     *
     * @param azEncourage the azEncourage to create
     * @return the ResponseEntity with status 201 (Created) and with body the new azEncourage, or with status 400 (Bad Request) if the azEncourage has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/az-encourages")
    @Timed
    public ResponseEntity<AzEncourage> createAzEncourage(@Valid @RequestBody AzEncourage azEncourage) throws URISyntaxException {
        log.debug("REST request to save AzEncourage : {}", azEncourage);
        if (azEncourage.getId() != null) {
            throw new BadRequestAlertException("A new azEncourage cannot already have an ID", ENTITY_NAME, "idexists");
        }

        AzEncourage result = azEncourageService.createAzEncourageAndCash(azEncourage);
        return ResponseEntity.created(new URI("/api/az-encourages/" + azEncourage.getId().toString()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, azEncourage.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /az-encourages : Updates an existing azEncourage.
     *
     * @param azEncourage the azEncourage to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated azEncourage,
     * or with status 400 (Bad Request) if the azEncourage is not valid,
     * or with status 500 (Internal Server Error) if the azEncourage couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/az-encourages")
    @Timed
    public ResponseEntity<AzEncourage> updateAzEncourage(@Valid @RequestBody AzEncourage azEncourage) throws URISyntaxException {
        log.debug("REST request to update AzEncourage : {}", azEncourage);
        if (azEncourage.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        AzEncourage result = azEncourageService.updateAzEncourageAndCash(azEncourage);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, azEncourage.getId().toString()))
            .body(result);
    }

    /**
     * GET  /az-encourages : get all the azEncourages.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of azEncourages in body
     */
    @GetMapping("/az-encourages")
    @Timed
    public List<AzEncourage> getAllAzEncourages() {
        log.debug("REST request to get all AzEncourages");
        return azEncourageRepository.findAll();
    }

    /**
     * GET  /az-encourages/:id : get the "id" azEncourage.
     *
     * @param id the id of the azEncourage to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the azEncourage, or with status 404 (Not Found)
     */
    @GetMapping("/az-encourages/{id}")
    @Timed
    public ResponseEntity<AzEncourage> getAzEncourage(@PathVariable Long id) {
        log.debug("REST request to get AzEncourage : {}", id);
        Optional<AzEncourage> azEncourage = azEncourageRepository.findById(id);
        return ResponseUtil.wrapOrNotFound(azEncourage);
    }

    /**
     * DELETE  /az-encourages/:id : delete the "id" azEncourage.
     *
     * @param id the id of the azEncourage to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/az-encourages/{id}")
    @Timed
    public ResponseEntity<Void> deleteAzEncourage(@PathVariable Long id) {
        log.debug("REST request to delete AzEncourage : {}", id);

        azEncourageService.deleteAzEncourageAndCash(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/az-encourages?query=:query : search for the azEncourage corresponding
     * to the query.
     *
     * @param query the query of the azEncourage search
     * @return the result of the search
     */
    @GetMapping("/_search/az-encourages")
    @Timed
    public List<AzEncourage> searchAzEncourages(@RequestParam String query) {
        log.debug("REST request to search AzEncourages for query {}", query);
        return StreamSupport
            .stream(azEncourageSearchRepository.search(queryStringQuery(query)).spliterator(), false)
            .collect(Collectors.toList());
    }

}
